﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Data;
using Ultima.Xna.Extensions;
using Ultima.Xna.Graphics.UI.Gumps;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI.Presets
{
    public class DialogGump : Gump
    {
        public static DialogGump Show(Game game, string text)
        {
            return Show(game, text, null);
        }

        public static DialogGump Show(Game game, string text, EventHandler<MouseEventArgs> closeCallback)
        {
            IUIService ui = game.Services.GetService<IUIService>(true);

            DialogGump dialog = ui.CreateInstance<DialogGump>("dialoggump", true);
            dialog.Text = text;
            dialog.MouseClick += closeCallback;

            return dialog;
        }

        Effect _effect;
        GumpButton _closeButton;
        GumpBackground _background;

        string _text = string.Empty;
        int _padding = 30;

        public override string Text
        {
            get { return base.Text; }
            set 
            {
                base.Text = value;

                string text = Utility.WrapASCIIText(9, base.Text, Size.X - (2 * _padding));

                int lineCount = 1;

                for (int i = 0; i < text.Length; i++)
                {
                    if (text[i] == '\n')
                        lineCount++;
                }

                Size = new Vector2(Size.X, (ASCIIFont.GetFixed(9).Height + 3) * lineCount + (2 * _padding) + 25);

                if (_closeButton != null)
                {
                    _closeButton.Position = new Vector2(Size.X / 2 - _closeButton.Size.X / 2, Size.Y - 45);
                }
            }
        }

        public override Vector2 Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;

                if (_background != null)
                {
                    _background.Size = value;
                }
            }
        }

        public DialogGump(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            Alignment = Alignment.MiddleMiddle;

            _background = new GumpBackground(Game);
            _background.GumpId = 0x242C;
            _background.Manager = Manager;
            _background.Initialize();

            Children.Add(_background);

            _closeButton = new GumpButton(Game);
            _closeButton.NormalId = 0xFB1;
            _closeButton.PressedId = 0xFB3;
            _closeButton.Manager = Manager;
            _closeButton.Initialize();
            _closeButton.MouseClick += new EventHandler<Ultima.Xna.Input.MouseEventArgs>(_closeButton_MouseClick);

            _effect = Game.Content.Load<Effect>("Shaders\\Gumps");

            Children.Add(_closeButton);

            Size = new Vector2(350, 50);
        }

        private void _closeButton_MouseClick(object sender, Ultima.Xna.Input.MouseEventArgs e)
        {
            OnMouseClicked(sender, e);

            this.Close();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            //ControlRenderer.RenderASCIIText(Manager.SpriteBatch, Position + new Vector2(_padding), new Rectangle(0, 0, Bounds.Width - (2 * _padding), 0), _effect, 9, _text);
        }
    }
}
